/**
 * The Free Gaming System; match data handling functions.
 *
 * Copyright 2006 the Free Gaming System developers.
 *
 * FGS is the legal property of its developers, whose names are too
 * numerous to list here.  Please refer to the COPYRIGHT file
 * distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include "fgs_game.h"
#include "fgs_internal.h"

fgs_error_t
fgs_new_match(fgs_ctx_t *ctx, fgs_uuid_t *uuid)
{
  /* Validate parameters */
  if (ctx == NULL || uuid == NULL)
    return FGS_ERR_INVALID_PARAMS;

  return fgs__proxy_new_match(ctx, uuid);
}


fgs_error_t
fgs_end_match(fgs_ctx_t *ctx, fgs_uuid_t match_uuid)
{
  /* Validate parameters */
  if (ctx == NULL || match_uuid == NULL)
    return FGS_ERR_INVALID_PARAMS;

  return fgs__proxy_del_match(ctx, match_uuid);
}


fgs_error_t
fgs_update_match (fgs_ctx_t *ctx, fgs_uuid_t match_uuid, char *key,
                  enum fgs_value_type value_type, ...)
{
  fgs_error_t err;
  va_list ap;

  /* Validate parameters */
  if (ctx == NULL || match_uuid == NULL || key == NULL)
    return FGS_ERR_INVALID_PARAMS;

  va_start(ap, value_type);
  err = fgs__proxy_update(ctx, "MATCHUPDATE", match_uuid, key, value_type, ap);
  va_end(ap);

  return err;
}


fgs_error_t
fgs_update_match_player (fgs_ctx_t *ctx, fgs_uuid_t match_uuid,
                         fgs_uuid_t player_uuid, char *key,
                         enum fgs_value_type value_type, ...)
{
  fgs_uuid_t tuple_uuid;
  int match_uuid_len, player_uuid_len;
  fgs_error_t err;
  va_list ap;

  /* Validate parameters */
  if (ctx == NULL || match_uuid == NULL ||
      player_uuid == NULL || key == NULL)
    return FGS_ERR_INVALID_PARAMS;

  /* This is a HACK. To avoid having to define another proxy func to
   * handle two UUIDs, we just concatenate the two with an \n in
   * between. This is a failure of encapsulation, as the abstraction
   * leaks. But until someone comes up with a better idea, this is the
   * best I could find.
   *
   * Actually, this is also fairly sucky in performance terms, for the
   * alloc/free. This is a real TODO if you want to improve the FGS.
   */
  match_uuid_len = strlen(match_uuid);
  player_uuid_len = strlen(player_uuid);
  tuple_uuid = fgs__alloc(match_uuid_len + player_uuid_len + 2, sizeof(char));

  memcpy(tuple_uuid, match_uuid, match_uuid_len);
  memcpy(&tuple_uuid[match_uuid_len+1], player_uuid, player_uuid_len);
  tuple_uuid[match_uuid_len] = '\n';

  va_start(ap, value_type);
  err = fgs__proxy_update(ctx, "MATCHPLAYERUPDATE", tuple_uuid, key,
                          value_type, ap);
  fgs__free(tuple_uuid);
  va_end(ap);

  return err;
}


fgs_error_t
fgs_join_match(fgs_ctx_t *ctx, fgs_uuid_t match_uuid, fgs_uuid_t player_uuid)
{
  /* Validate parameters */
  if (ctx == NULL || match_uuid == NULL || player_uuid == NULL)
    return FGS_ERR_INVALID_PARAMS;

  return fgs__proxy_add_to_match(ctx, match_uuid, player_uuid);
}


fgs_error_t
fgs_leave_match(fgs_ctx_t *ctx, fgs_uuid_t match_uuid, fgs_uuid_t player_uuid)
{
  /* Validate parameters */
  if (ctx == NULL || match_uuid == NULL || player_uuid == NULL)
    return FGS_ERR_INVALID_PARAMS;

  return fgs__proxy_del_from_match(ctx, match_uuid, player_uuid);
}


fgs_error_t
fgs_commit_match(fgs_ctx_t *ctx, fgs_uuid_t match_uuid)
{
  /* Validate parameters */
  if (ctx == NULL)
    return FGS_ERR_INVALID_PARAMS;

  return fgs__proxy_commit_match(ctx, match_uuid);
}
